/*
 * 文件名: PASafeKeyBoardEditText.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2012-2-8
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.PASafeKeyBoard;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import app.widget.PASafeKeyBoard.PAKeyBoardEditText.OnBtnShowStateListener;
import app.widget.PASafeKeyBoard.PAKeyBoardEditText.OnBtnSureClickListener;
import app.widget.PASafeKeyBoard.PAKeyBoardEditText.OnEditFocusListener;
import app.widget.PASafeKeyBoard.PAKeyBoardEditText.OnEditOnClickListener;
import app.widget.PASafeKeyBoard.PAKeyBoardEditText.OnHideKeyBoardListener;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2012-2-8
 * @version [Android PABank C01, @2012-2-8]
 * @description
 */
public class KeyBoardEditText extends LinearLayout {
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 自定义EditText
	 */
	private PAKeyBoardEditText keyBoardEditText;
	/**
	 * 删除按钮
	 */
	private ImageButton editDelete;
	/**
	 * 是否以零开头
	 */
	private boolean isStartZero = true;
	/**
	 * 是否为小数
	 */
	private boolean isDecimal = false;
	/**
	 * 是否为密码
	 */
	private boolean isPassword = false;
	/**
	 * 输入方式 0 数字 1小数
	 */
	private int inputMethod;
	/**
	 * 小数的长度
	 */
	private int decimalLenght;
	/**
	 * view
	 */
	private View mLinearLayout;
	/**
	 * focus_true
	 */
	private int focusDrawableId = -1;
	/**
	 * focus_false
	 */
	private int noFocusDrawableId = -1;
	/**
	 * 默认图片
	 */
	private int focusStates[] = new int[] { R.drawable.app_widget_input_box_elipse,
			R.drawable.app_widget_input_box_elipse_c };
	/**
	 * 设置带叉的按钮
	 */
	private boolean isEditDelete = true;

	public boolean isEditDelete() {
		return isEditDelete;
	}

	public void setEditDelete(boolean isEditDelete) {
		this.isEditDelete = isEditDelete;
		if(!isEditDelete) {
			editDelete.setVisibility(View.GONE);
		}
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @return int 得到小数的长度
	 */
	public int getDecimalLenght() {
		return decimalLenght;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置小数的长度
	 * 
	 * @param decimalLenght
	 *            小数的长度
	 */
	public void setDecimalLenght(int decimalLenght) {
		this.decimalLenght = decimalLenght;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]得到聚焦时drawable id
	 * 
	 * @return int
	 */
	public int getFocusDrawableId() {
		return focusDrawableId;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]设置聚焦时drawable id
	 * 
	 * @param focusDrawableId
	 *            聚焦时drawable id
	 */
	public void setFocusDrawableId(int focusDrawableId) {
		this.focusDrawableId = focusDrawableId;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 得到未聚焦时drawable id
	 * 
	 * @return int
	 */
	public int getNoFocusDrawableId() {
		return noFocusDrawableId;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置未聚焦时drawable id
	 * 
	 * @param noFocusDrawableId
	 *            未聚焦时drawable id
	 */
	public void setNoFocusDrawableId(int noFocusDrawableId) {
		this.noFocusDrawableId = noFocusDrawableId;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]得到文本值
	 * 
	 * @return Editable
	 */
	public Editable getText() {
		return keyBoardEditText.getText();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置文本值
	 * 
	 * @param text
	 */
	public void setText(CharSequence text) {
		keyBoardEditText.setText(text);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置文本值
	 * 
	 * @param text
	 */
	public void setText(int resId) {
		keyBoardEditText.setText(resId);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 得到hint值
	 * 
	 * @return CharSequence
	 */
	public CharSequence getHint() {
		return keyBoardEditText.getHint();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置hint值
	 * 
	 * @param hint
	 */
	public void setHint(CharSequence hint) {
		keyBoardEditText.setHint(hint);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置hint值
	 * 
	 * @param hint
	 */
	public void setHint(int resId) {
		keyBoardEditText.setHint(resId);

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置颜色
	 * 
	 * @param color
	 */

	public void setTextColor(int color) {
		keyBoardEditText.setTextColor(color);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置hint颜色
	 * 
	 * @param color
	 */

	public void setHintTextColor(int color) {
		keyBoardEditText.setHintTextColor(color);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置字体大小
	 * 
	 * @param color
	 */

	public void setTextSize(float size) {
		keyBoardEditText.setTextSize(size);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置单行
	 * 
	 * @param color
	 */

	public void setSingleLine(boolean singleLine) {
		keyBoardEditText.setSingleLine(singleLine);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置focus
	 * 
	 * @param color
	 */

	public void setFocusable(boolean focusable) {
		keyBoardEditText.setFocusable(focusable);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置最大长度
	 * 
	 * @param maxLength
	 *            最大长度
	 */

	public void setMaxLength(int maxLength) {
		if (maxLength > 0)
			keyBoardEditText
					.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
							maxLength) });
		keyBoardEditText.setIntergerLength(maxLength);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 是否以零开头
	 * 
	 * @return boolean
	 */
	public boolean isStartZero() {
		return keyBoardEditText.isStartZero();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置是否以零开头
	 * 
	 * @param isStartZero
	 *            是否以零开头
	 */
	public void setStartZero(boolean isStartZero) {
		keyBoardEditText.setStartZero(isStartZero);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 是否为小数
	 * 
	 * @return boolean
	 */
	public boolean isDecimal() {
		return keyBoardEditText.isDecimal();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置是否为小数
	 * 
	 * @param isDecimal
	 *            是否为小数
	 */
	public void setDecimal(boolean isDecimal) {
		keyBoardEditText.setDecimal(isDecimal);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 是否为密码
	 * 
	 * @return boolean
	 */
	public boolean isPassword() {
		return keyBoardEditText.isPassword();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置是否为密码
	 * 
	 * @param isPassword
	 *            是否为密码
	 */
	public void setPassword(boolean isPassword) {
		keyBoardEditText.setPassword(isPassword);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]得到输入方式
	 * 
	 * @return int
	 */
	public int getInputMethod() {
		return keyBoardEditText.getInputMethod();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 设置输入方式
	 * 
	 * @param inputMethod
	 *            输入方式
	 */
	public void setInputMethod(int inputMethod) {
		keyBoardEditText.setInputMethod(inputMethod);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param enabled
	 * @see android.view.View#setEnabled(boolean)
	 */
	public void setEnabled(boolean enabled) {
		keyBoardEditText.setEnabled(enabled);
		if (!enabled) {
			if(isEditDelete) {
				editDelete.setVisibility(View.GONE);
			}
			
			keyBoardEditText.setTextColor(Color.WHITE);
		} else {
			keyBoardEditText.setTextColor(Color.BLACK);
			if (keyBoardEditText.getText().toString().length() > 0) {
				if(isEditDelete) {
					editDelete.setVisibility(View.VISIBLE);
				}
				
			}
		}
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param enabled
	 *            是否可編輯
	 * @param enabledTextColor
	 *            設置顏色
	 */
	public void setEnabledTextColor(boolean enabled, int enabledTextColor) {
		keyBoardEditText.setEnabled(enabled);
		keyBoardEditText.setTextColor(enabledTextColor);
		if (!enabled) {
			if(isEditDelete) {
				editDelete.setVisibility(View.GONE);
			}
			
		} else {
			if (keyBoardEditText.getText().toString().length() > 0) {
				if(isEditDelete) {
					editDelete.setVisibility(View.VISIBLE);
				}
			}
		}

	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public KeyBoardEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
		TypedArray type = context.obtainStyledAttributes(attrs,
				R.styleable.PAEditTextStyle);

		focusDrawableId = type.getResourceId(
				R.styleable.PAEditTextStyle_focusDrawable, -1);

		noFocusDrawableId = type.getResourceId(
				R.styleable.PAEditTextStyle_noFocusDrawable, -1);
		if (noFocusDrawableId != -1) {
			mLinearLayout.setBackgroundResource(noFocusDrawableId);
		} else {
			mLinearLayout.setBackgroundResource(focusStates[0]);

		}

		int textSize = type.getInt(R.styleable.PAEditTextStyle_textSize, -1);
		if (textSize > 0)
			keyBoardEditText.setTextSize(textSize);
		else {
			keyBoardEditText.setTextSize(14);
		}

		int textColor = type.getColor(R.styleable.PAEditTextStyle_textColor,
				0xFFFFFFFF);
		if (textColor > 0)
			keyBoardEditText.setTextColor(textColor);

		String text = type.getString(R.styleable.PAEditTextStyle_text);
		if (text != null)
			keyBoardEditText.setText(text);

		String hint = type.getString(R.styleable.PAEditTextStyle_hint);
		keyBoardEditText.setHint(hint);

		int hintTextColor = type.getColor(
				R.styleable.PAEditTextStyle_hintTextColor, 0xFFFFFFFF);
		if (hintTextColor > 0)
			keyBoardEditText.setHighlightColor(hintTextColor);

		int inputMethod = type.getInt(R.styleable.PAEditTextStyle_inputMethod,
				-1);
		keyBoardEditText.setInputMethod(inputMethod);

		boolean isStartZero = type.getBoolean(
				R.styleable.PAEditTextStyle_isStartZero, true);
		keyBoardEditText.setStartZero(isStartZero);

		boolean singleLine = type.getBoolean(
				R.styleable.PAEditTextStyle_singleLine, true);
		keyBoardEditText.setSingleLine(singleLine);

		boolean decimal = type.getBoolean(R.styleable.PAEditTextStyle_decimal,
				false);
		keyBoardEditText.setDecimal(decimal);

		int decimalLength = type.getInt(
				R.styleable.PAEditTextStyle_decimalLength, 2);
		keyBoardEditText.setDecimalLength(decimalLength);

		boolean password = type.getBoolean(
				R.styleable.PAEditTextStyle_password, false);
		keyBoardEditText.setPassword(password);

		int maxLength = type.getInt(R.styleable.PAEditTextStyle_maxLength, -1);
		if (maxLength > 0) {

			if (decimal && decimalLength > 0) {
				keyBoardEditText
						.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
								maxLength + decimalLength + 1) });
			} else {
				keyBoardEditText
						.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
								maxLength) });
			}

			keyBoardEditText.setIntergerLength(maxLength);
		}
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public KeyBoardEditText(Context context) {
		super(context);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化操作
	 */
	private void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		mLinearLayout = inflater.inflate(R.layout.app_widget_keyboard_linearlayout, null);
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		mLinearLayout.setLayoutParams(lp);
		addView(mLinearLayout);

		keyBoardEditText = (PAKeyBoardEditText) mLinearLayout
				.findViewById(R.id.keyboardEditText);
		editDelete = (ImageButton) mLinearLayout.findViewById(R.id.edit_delete);
		editDelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				keyBoardEditText.setText("");
				if (onLinearLayoutOnClickListener != null) {
					onLinearLayoutOnClickListener
							.onLinearLayoutOnClick(KeyBoardEditText.this);
				}
			}
		});
		keyBoardEditText
				.setOnBtnShowStateListener(new OnBtnShowStateListener() {

					@Override
					public void onBtnShowState(boolean showState) {
						// PABankLog.i("zbkc", "zbkc :" + showState);
						if(isEditDelete) {
							if (showState) {
								editDelete.setVisibility(View.VISIBLE);
							} else {
								editDelete.setVisibility(View.GONE);
							}
						}
						
					}
				});

		keyBoardEditText.setOnEditFocusListener(new OnEditFocusListener() {

			@Override
			public void onEditFocus(boolean hasFocus) {

				if (hasFocus) {
					if (keyBoardEditText.getText().toString() != null
							&& !"".equals(keyBoardEditText.getText().toString())) {
						if(isEditDelete) {
							editDelete.setVisibility(View.VISIBLE);
						}
					}
					if (focusDrawableId != -1) {
						mLinearLayout.setBackgroundResource(focusDrawableId);

					} else {
						mLinearLayout.setBackgroundResource(focusStates[1]);

					}
				} else {
					if(isEditDelete) {
						editDelete.setVisibility(View.GONE);
					}
					if (noFocusDrawableId != -1) {
						mLinearLayout.setBackgroundResource(noFocusDrawableId);
					} else {
						mLinearLayout.setBackgroundResource(focusStates[0]);
					}
				}

			}
		});

		keyBoardEditText.setOnEditOnClickListener(new OnEditOnClickListener() {

			@Override
			public void onEditOnClick() {
				if (onLinearLayoutOnClickListener != null) {
					onLinearLayoutOnClickListener
							.onLinearLayoutOnClick(KeyBoardEditText.this);
				}

			}

		});

		keyBoardEditText
				.setOnHideKeyBoardListener(new OnHideKeyBoardListener() {
					@Override
					public void onHideKeyBoard(boolean isHideKeyBoard) {
						if (onHideKeyBoardsListener != null) {
							onHideKeyBoardsListener
									.onHideKeyBoards(isHideKeyBoard);
						}
					}
				});

		keyBoardEditText
				.setOnBtnSureClickListener(new OnBtnSureClickListener() {

					@Override
					public void onBtnSureClick() {
						if (onKeyBoardBtnSureClickListener != null) {
							onKeyBoardBtnSureClickListener.onKeyBoardBtnSure();
						}
					}
				});

	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 隐藏键盘
	 */

	public void hideKeyBoard() {
		keyBoardEditText.hideKeyBoard();
	}

	public interface OnLinearLayoutOnClickListener {
		void onLinearLayoutOnClick(View view);
	}

	private OnLinearLayoutOnClickListener onLinearLayoutOnClickListener;

	public void setOnLinearLayoutOnClickListener(
			OnLinearLayoutOnClickListener onLinearLayoutOnClickListener) {
		this.onLinearLayoutOnClickListener = onLinearLayoutOnClickListener;
	}

	public interface OnHideKeyBoardsListener {
		void onHideKeyBoards(boolean isHideKeyBoard);
	}

	private OnHideKeyBoardsListener onHideKeyBoardsListener;

	public void setOnHideKeyBoardsListener(
			OnHideKeyBoardsListener onHideKeyBoardsListener) {
		this.onHideKeyBoardsListener = onHideKeyBoardsListener;
	}

	public interface OnKeyBoardBtnSureClickListener {
		void onKeyBoardBtnSure();
	}

	private OnKeyBoardBtnSureClickListener onKeyBoardBtnSureClickListener;

	public void setOnKeyBoardBtnSureClickListener(
			OnKeyBoardBtnSureClickListener onKeyBoardBtnSureClickListener) {
		this.onKeyBoardBtnSureClickListener = onKeyBoardBtnSureClickListener;
	}

	public OnKeyBoardBtnSureClickListener getOnKeyBoardBtnSureClickListener() {
		return onKeyBoardBtnSureClickListener;
	}
}
